
Imports std
Imports System.IO

Module diffline

    Public Const maxInputCount As Int32 = 1024

    Public Class arguments
        Public Const casesense As String = "/casesense"
    End Class

    Sub Main(ByVal args() As String)
        On Error Resume Next
        Dim reader(maxInputCount) As TextReader
        Dim writer As TextWriter = Nothing
        Dim input(maxInputCount) As String
        Dim i As Int32
        Dim inputIndex As Int32
        Dim casesense As Boolean = False

        If args.Length < 3 Then
            raiseError("argument count error, [/casesense] input1 input2 ... inputN ... output, " + _
                        "if input/output is '/-', use console", errorHandle.errorType.critical)
        Else
            If args(args.Length - 1) = "/-" Then
                writer = Console.Out
            Else
                writer = New StreamWriter(New _
                            FileStream(args(args.Length - 1), FileMode.Create, FileAccess.Write, FileShare.Read))
            End If
            assert(Not writer Is Nothing, "file no found, " + args(args.Length - 1))

            For i = 0 To args.Length - 1
                If strleft(args(i), 1) = character.leftSlash Then
                    Select Case args(i).ToLower
                        Case arguments.casesense
                            casesense = True
                    End Select
                Else
                    Exit For
                End If
            Next

            inputIndex = i
            For i = inputIndex To args.Length - 2
                If args(i) = "/-" Then
                    reader(i) = Console.In
                Else
                    reader(i) = New StreamReader(New FileStream(args(i), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                End If
                assert(Not reader(i) Is Nothing, "file no found, " + args(i))
            Next

            For i = inputIndex To args.Length - 2
                input(i) = reader(i).ReadLine
            Next
            While Not containNothing(input, inputIndex, args.Length - 1)
                For i = inputIndex + 1 To args.Length - 2
                    If Not strsame(input(inputIndex), input(i), casesense) Then
                        Exit For
                    End If
                Next

                If i < args.Length - 1 Then
                    For i = inputIndex To args.Length - 3
                        writer.Write(input(i))
                        writer.Write(character.tab)
                    Next
                    writer.WriteLine(input(i))
                End If

                For i = inputIndex To args.Length - 2
                    input(i) = reader(i).ReadLine
                Next
            End While

            For i = inputIndex To args.Length - 2
                closestream(reader(i))
            Next
            closestream(writer)
        End If
    End Sub

End Module
